﻿<cfcomponent>
<cfscript>
DB = 'chinalaohu';
dbt = 'auction_userwonpaid';
domain = 'http://img1.chinalaohu.com';
</cfscript>
<cfparam name="UUID" default="0" type="string" >
<cfparam name="platform" default="0" >


<!--- 判断当前登录手机是安卓1还是苹果2 没查到0--->
<cffunction access="remote" name="checkPhone" returnFormat="plain">
	<cfquery datasource="#DB#" name="rs">
		select imsi from user_login where uid='#uid#' order by loginDT desc limit 1
	</cfquery>
	<cfif rs.recordcount lt 1>
		<cfreturn 0>
	<cfelseif rs.imsi neq ''>
		<cfreturn 1>
	<cfelse>
		<cfreturn 2>
	</cfif>
</cffunction>

<!---支付商品选择接口 --->
<cffunction access="remote" name="userreadypaid" returnFormat="json">
  <cfscript>
	j = {"success":"0"};
	</cfscript>
  
	<!---根据UUID查找ID--->
  <cfinvoke component="app.cfc.user" method="uuidgetuid" returnvariable="uuidgetuid" UUID="#uuid#">
  <cfif uuidgetuid.recordcount neq 1>
  	<cfscript>
		//,"msg":"用户ID错误"
		j = {"success":"0","errCode":"0"};
		</cfscript>
  <cfelse>
  	<cfif isdefined('aid') and listlen(aid) gte 1>
    	<!---检测拍品是否未加入订单--->
      <cfinvoke component="userorder" method="checkaidnoorder" returnvariable="checkaidnoorder" uid="#uuidgetuid.uid#" aidlist="#aid#" />
    	<cfif checkaidnoorder.recordcount eq 0>
				<cfscript>
        //,"msg":"拍品不存在或已经被加入其他单子"
        j = {"success":"0","errCode":"-1"};
        </cfscript>
        
      <cfelse>
      	<cfscript>
				sumbid = 0;
				sumaweight = 0;
				money = 0;
				carriage = 0;
				points = 0;
				needPay = 0;
				serviceCharge = 0;
				despatchType = arraynew(1);
        </cfscript>
      	<!---会员收货地址--->
        <cfquery datasource="#DB#" name="defaultaddr">
        select *
        from user_addresses
        where uid = '#uuidgetuid.uid#'
        order by set_default DESC
        limit 1
        </cfquery>
        <!---计算金额等--->
        <cfoutput query="checkaidnoorder">
        	<cfscript>
					sumbid = sumbid + bid;
					fwf = bid * servicefee*0.01;
					serviceCharge = serviceCharge + fwf;
					//money = money + bid + fwf;
					money = money + bid;
					if (isnumeric(aweight)){
					sumaweight = sumaweight + aweight;
					}
					</cfscript>
        </cfoutput>
        
        <!---物流方式--->
        <cfquery datasource="#DB#" name="shipping">
        select shipping_id,shipping_name
        from set_shipping
        where enabled = 1 and shipping_id='2' order by shipping_id desc    
        </cfquery>        
        <cfoutput query="shipping">
        <cfscript>
				despatchjson = {
					"key": shipping_id,
					"value": shipping_name
				};				
				arrayappend(despatchType,despatchjson);
        </cfscript>
        </cfoutput>
        
        
        <cfscript>
				//计算费用的地址
				defProvince = '';
				//地址
				if (defaultaddr.recordcount eq 0){
					addressId = 0;
					address = '';
				}
				else {
					defProvince = defaultaddr.province;
					addressId = defaultaddr.uaid;
					address = defaultaddr.province & defaultaddr.city & defaultaddr.district & ' ' & defaultaddr.street;
					

					
				}
		</cfscript>
				<!--- 通过用户主键，货品主键序列，获取其默认发货地址的运费 --->
				<!--- 计算货品总重量 --->
				<cfquery datasource="#DB#" name="tweight">
					select sum(aweight) as total_weight from auction where aid in (#aid#)
				</cfquery>
							
				<!--- 调用存储过程计算金额 --->
				<cfstoredproc procedure="pro_get_yunfei" dataSource="#DB#"> 
					<cfprocparam type="IN" CFSQLType="cf_sql_varchar" value="#tweight.total_weight#" > 
					<cfprocparam type="IN" CFSQLType="cf_sql_varchar" value="#defProvince#" > 
					<cfprocparam type="OUT" CFSQLType="cf_sql_integer" variable="weight_money"> 
				</cfstoredproc> 
				<cfinvoke method="checkPhone" returnvariable="phoneType" uid="#uuidgetuid.uid#" />
		<cfscript>
				//积分
				//points = sumbid \ 100; 只计入服务费方式
				points = money \ 100;
				
				needPay = money + serviceCharge;
				
				if(phoneType eq 1){
					//安卓手机
					needPay = needPay + weight_money;
				}else{
					//苹果手机
				}
				
				//为了安卓系统中，无法通过0来识别处理
				if(addressId eq "0"){
					addressId = "";
				}
				
				goodslist = valuelist(checkaidnoorder.auction);
				orderjson = {
					"addressId":addressId,
					"address":address, 
					"payType":[{"key":"6","value":"银联移动支付"}],
					"count":checkaidnoorder.recordcount,
					"money":money,
					"carriage":weight_money,
					"points":points,
					"needPay":needPay,
					"serviceCharge":serviceCharge,
					"goodsList":listtoarray(goodslist),
					"despatchType":despatchType
				};
				
				j = {"success":"1","order":orderjson};
				</cfscript>
        
        
      </cfif>
    
    <cfelse>
			<cfscript>
      //,"msg":"未选择拍品"
      j = {"success":"0","errCode":"-1"};
      </cfscript>
    </cfif>
    
  </cfif>

  <cfscript>
	j = createObject("component",'cfjson').encode(j);
  return j;
	</cfscript>
</cffunction>


<!---订单确认提交接口 --->
<cffunction access="remote" name="userSubmitPaid" returnFormat="json">
	<cfscript>
		j = {"success":"0"};
	</cfscript>
	<!---根据UUID查找ID--->
	<cfinvoke component="app.cfc.user" method="uuidgetuid" returnvariable="uuidgetuid" UUID="#uuid#">
	<cfif uuidgetuid.recordcount neq 1>
		<cfscript>
			//,"msg":"用户ID错误"
			j = {"success":"0","errCode":"0"};
		</cfscript>
	<cfelse>
		<cfparam name="carriage" default="0.00" type="numeric">
		<!---是否已经下了订单--->  
		<cfif isdefined('orderid')>
			<cfquery datasource="#DB#" name="showneworder">
				  select *
				  from #dbt#
				  where paidid = #orderid#
				  </cfquery>
				  <cfquery datasource="#DB#" name="orderwon">
				  select count(1) as rc
				  from auction_userwon
				  where paidid = #orderid#
			</cfquery>
			<cfparam name="tnid" default="">
			<cfscript>
				//if (despatchType eq 1 and carriage eq 0){
				//	address = '上门自提';
				//}
				//else {
					address = showneworder.ship_area & ' ' & showneworder.ship_addr;
				//}

			  j = {
			  "success":"1",
			  "orderId":orderid,
			  "tnId":tnId,
			  "orderTime":datetimeformat(showneworder.acttime,'yyyy-mm-dd HH:NN:SS'),
			  "address":address,
			  "payType":showneworder.payment,
			  "pay":showneworder.final_amount,
			  "goodsCount":orderwon.rc
			  };
			</cfscript>
		<cfelse>
			<!---会员收货地址--->
			<cfquery datasource="#DB#" name="defaultaddr">
				select *
				from user_addresses
				where uid = '#uuidgetuid.uid#'
				order by set_default DESC
				limit 1
			</cfquery>
			<cfscript>
					//计算费用的地址
					defProvince = '';
					//地址
					if (defaultaddr.recordcount eq 0){

					}else {
						defProvince = defaultaddr.province;
					}
			</cfscript>
			<!--- 通过用户主键，货品主键序列，获取其默认发货地址的运费 --->
			<!--- 计算货品总重量 --->
			<cfquery datasource="#DB#" name="tweight">
				select sum(aweight) as total_weight from auction where aid in (#aid#)
			</cfquery>
			<!--- 调用存储过程计算金额 --->
			<cfstoredproc procedure="pro_get_yunfei" dataSource="#DB#"> 
				<cfprocparam type="IN" CFSQLType="cf_sql_varchar" value="#tweight.total_weight#" > 
				<cfprocparam type="IN" CFSQLType="cf_sql_varchar" value="#defProvince#" > 
				<cfprocparam type="OUT" CFSQLType="cf_sql_integer" variable="weight_money"> 
			</cfstoredproc>
			<!---aid是否有效列表--->
			<cfif isdefined('aid') and listlen(aid) gt 0>
				<cfparam name="despatchType" default="1">
				<cfparam name="payType" default="6">
				<cfparam name="remarks" default="">
				<cfif not isdefined('addressId')>
					<cfscript>
						//,"msg":"未选择地址"
						j = {"success":"0","errCode":"-1","errMsg":"未选择地址"};
					</cfscript>
				<cfelse>
					<cfscript>
						//苹果手机，传给服务端的运费实际上是服务费，需要重新计算一下，程序中又无法判断出安卓还是
						//苹果
						carriage = weight_money;
					</cfscript>
					<cfinvoke component="userwonpaid" method="createwonpaid" returnvariable="paidid" 
					uid="#uuidgetuid.uid#" mobile="#uuidgetuid.mobile#" fare="#carriage#" uaid="#addressId#" aidlist="#aid#" pay_id="#payType#" remark="#remarks#" />
					<!---是否成功--->
					<cfif isnumeric(paidid)>
						<!---显示刚创建的定单--->
						<cfquery datasource="#DB#" name="showneworder">
							  select *
							  from #dbt#
							  where paidid = #paidid#
							  </cfquery>
							  <cfquery datasource="#DB#" name="orderwon">
							  select count(1) as rc
							  from auction_userwon
							  where paidid = #paidid#
						</cfquery>
						<cfscript>
							//if (despatchType eq 1 and carriage eq 0){
							//	address = '上门自提';
							//}
							//else {
								address = showneworder.ship_area & ' ' & showneworder.ship_addr;
							//}
							  j = {
							  "success":"1",
							  "orderId":paidid,
							  "tnId":showneworder.tnId,
							  "orderTime":datetimeformat(showneworder.acttime,'yyyy-mm-dd HH:NN:SS'),
							  "address":address,
							  "payType":showneworder.payment,
							  "pay":showneworder.final_amount,
							  "goodsCount":orderwon.rc,
										"remarks":showneworder.remark
							  };
						</cfscript>
					<cfelse>
						<cfscript>
							//,"msg":"交易单创建失败"
							j = {"success":"0","errCode":"-1","errMsg":"交易单创建失败"};
						</cfscript>
					</cfif>
				</cfif>
			<cfelse>
				<cfscript>
					//,"msg":"无有效拍品"
					j = {"success":"0","errCode":"-1","errMsg":"无效拍品"};
				</cfscript>
			</cfif>
		</cfif>
	</cfif>
	<cfscript>
		j = createObject("component",'cfjson').encode(j);
		return j;
	</cfscript>
</cffunction>

<!---未付款订单列表带明细功能--->
<cffunction access="remote" name="unpaidlistdetail" returntype="any" returnFormat="json">
	
  <cfscript>
	j = {"success":"0"};
	myOrder = arraynew(1);
	aidlist = '';
	</cfscript>
  
	<!---根据UUID查找ID--->
  <cfinvoke component="app.cfc.user" method="uuidgetuid" returnvariable="uuidgetuid" UUID="#uuid#">
  <cfif uuidgetuid.recordcount neq 1>
  	<cfscript>
		//,"msg":"用户ID错误"
		j = {"success":"0","errCode":"0"};
		</cfscript>
  <cfelse>
		<cfquery datasource="#DB#" name="unpaidlist">
    SELECT *
    FROM
    auction_userwonpaid
    WHERE
    auction_userwonpaid.uid = #uuidgetuid.uid# AND
    auction_userwonpaid.pay_status < 2
    AND auction_userwonpaid.acttime IS NOT NULL
    </cfquery>
    <cfif unpaidlist.recordcount gt 0>
    	<cfoutput query="unpaidlist">
      	<cfscript>
				list = arraynew(1);
				</cfscript>
        <cfquery datasource="#DB#" name="bidauction">
        SELECT
        auction_userwon.aid,
        auction_userwon.acode,
        auction_gallery.img_waterfall,
        auction_userwon.auction,
        auction_userwon.endtime,
        auction_userwon.bid_amount
        FROM
        auction_userwon
        INNER JOIN auction ON auction.aid = auction_userwon.aid
        INNER JOIN auction_gallery ON auction_gallery.agid = auction.agid
        WHERE
        auction_userwon.paidID = #PAIDID#      
        </cfquery>
        <cfloop query="bidauction">
        <cfscript>
				aidlist = listappend(aidlist,aid);
				switch(platform){
					case 0:
						iconUrl = domain & img_waterfall;
						if (left(img_waterfall,8) eq '/upload/' and left(img_waterfall,13) neq '/upload/adio/'){
							iconUrl = domain & replacenocase(img_waterfall,'/upload/','/upload/adio/');
						}
						break;
					case 1:
						iconUrl = domain & img_waterfall;
						if (left(img_waterfall,8) eq '/upload/' and left(img_waterfall,12) neq '/upload/ios/'){
							iconUrl = domain & replacenocase(img_waterfall,'/upload/','/upload/ios/');
						}
						break;
				}
        listjson = {
					"aid": aid,
					"auctionCode": acode,
					"iconUrl": iconUrl,
					"name": auction,
					"dealTime": datetimeformat(endtime,'yyyy-mm-dd HH:NN:SS'),
					"price": bid_amount
				};
				
				arrayappend(list,listjson);
				</cfscript>        
        </cfloop>
        <cfscript>
				myorderjson = {
					"aid":aidlist,
					"addressId":ship_addrid,
					"payType":payment,
					"orderId":paidid,
					"tnId":tnid,
					"orderTime":datetimeformat(acttime,'yyyy-mm-dd HH:NN:SS'),
					"address":ship_area & ' ' & ship_addr,
					"despatchType":shipment,
					"pay":numberformat(final_amount,'_.__'),
					"goodsCount":listlen(aidlist),
					"remarks":remark,
					"list":list					
				};
				arrayappend(myorder,myorderjson);
				</cfscript>
      
      </cfoutput> 
    </cfif>
    
    <cfscript>
		
		j = {"success":"1","myOrder":myOrder};
		</cfscript>
    
  </cfif>

  <cfscript>
	j = createObject("component",'cfjson').encode(j);
  return j;
	</cfscript>
</cffunction>

<!---取消订单接口--->
<cffunction access="remote" name="cancel" returntype="any" returnFormat="json">
	
  <cfscript>
	j = {"success":"0"};
	myOrder = arraynew(1);
	aidlist = '';
	</cfscript>
  
	<!---根据UUID查找ID--->
  <cfinvoke component="app.cfc.user" method="uuidgetuid" returnvariable="uuidgetuid" UUID="#uuid#">
  <cfif uuidgetuid.recordcount neq 1>
  	<cfscript>
		//,"msg":"用户ID错误"
		j = {"success":"0","errCode":"0"};
		</cfscript>
  <cfelse>
  	<cfif isdefined('orderid') and isnumeric(orderid)>
      <cfquery datasource="#DB#" name="checkordernoaction">
      SELECT
      paidID
      FROM
      auction_userwonpaid
      WHERE
      auction_userwonpaid.uid = #uuidgetuid.uid# AND
      auction_userwonpaid.task = 0 AND
      auction_userwonpaid.ftask = 0 AND
      auction_userwonpaid.payed = 0
      AND paidID = '#orderid#'
      </cfquery>
      <cfif checkordernoaction.recordcount gt 0>
      	<!---列出订单下的拍品--->
        <cfquery datasource="#DB#" name="checkorderitemlist">
        SELECT
        auction_userwon.wonID
        FROM
        auction_userwon
        WHERE
        auction_userwon.paidID = #orderid# AND
        auction_userwon.uid = #uuidgetuid.uid#
        </cfquery>
        <!---取消拍品加入状态--->
        <cfset wonidlist = valuelist(checkorderitemlist.wonid)>
        <cfif listlen(wonidlist) gt 0>
          <cfquery datasource="#DB#">
          update auction_userlastbid set
          wonid = 0
          where uid=#uuidgetuid.uid#
          and wonid in(#wonidlist#)
          </cfquery>
          <cfquery datasource="#DB#">
          delete from auction_userwon
          where uid=#uuidgetuid.uid#
          and wonid in(#wonidlist#)
        </cfquery>
        </cfif>
        <cfquery datasource="#DB#">
        delete from auction_userwonpaid
        WHERE
        auction_userwonpaid.uid = #uuidgetuid.uid# 
        AND paidID = '#orderid#'
        </cfquery>
        
				<cfscript>
        j = {"success":"1"};
        </cfscript>
      <cfelse>
				<cfscript>
        //,"msg":"没有找到附合的订单"
        j = {"success":"0","errCode":"-1"};
        </cfscript>
        
      </cfif>
    <cfelse>    
			<cfscript>
      //,"msg":"少ORDERID"
      j = {"success":"0","errCode":"-1"};
      </cfscript>
    </cfif>
  </cfif>

  <cfscript>
	j = createObject("component",'cfjson').encode(j);
  return j;
	</cfscript>
</cffunction>














<!---检测拍品是否未加入订单--->
<cffunction name="checkaidnoorder" access="remote" returntype="query">
	<cfquery datasource="#DB#" name="checkaidnoorder">
select a.aid,a.bid,b.servicefee,b.aweight,b.auction from auction_userlastbid  a 
 left outer join auction b on a.aid=b.aid   left outer join auction_special c on b.asid=c.asid  
 left outer join auction_userwon d on a.wonID=d.wonID left outer join auction_userwonpaid e on d.paidID=e.paidID 
 where a.uid = #uid#
  
   and a.is_bid=1 and a.pay_status=0 
   and a.aid in (#aidlist#)
 
  </cfquery>
	<cfreturn checkaidnoorder>

</cffunction>


</cfcomponent>